package com.zrf.gulimall.ware.controller;

import com.zrf.gulimall.commnon.core.to.SkuHasStockVo;
import com.zrf.gulimall.commnon.core.utils.R;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.ware.entity.WareSkuEntity;
import com.zrf.gulimall.ware.service.WareSkuService;
import com.zrf.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品库存
 *
 * @author zrf
 * @email 120662332@qq.com
 * @date 2021-01-23 17:34:49
 */
@RestController
@RequestMapping("/ware/waresku")
public class WareSkuController {
	@Autowired
	private WareSkuService wareSkuService;

	/**
	 * 锁定库存
	 * 库存解锁的场景
	 * 1）、下订单成功，订单过期没有支付被系统自动取消或者被用户手动取消，都要解锁库存
	 * 2）、下订单成功，库存锁定成功，接下来的业务调用失败，导致订单回滚。之前锁定的库存就要自动解锁
	 * 3）、
	 */
	@PostMapping(value = "/lock/order")
	public R orderLockStock(@RequestBody WareSkuLockVo vo) {
		boolean lockStock = wareSkuService.orderLockStock(vo);
		return R.ok().setData(lockStock);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("ware:waresku:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = wareSkuService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	//@RequiresPermissions("ware:waresku:info")
	public R info(@PathVariable("id") Long id) {
		WareSkuEntity wareSku = wareSkuService.getById(id);

		return R.ok().put("wareSku", wareSku);
	}

	/**
	 * 是否有库存
	 */
	@PostMapping("/hasstock")
	//@RequiresPermissions("ware:waresku:info")
	public R skuHasStock(@RequestBody List<Long> skuIdList) {
		List<SkuHasStockVo> hasStock = wareSkuService.skuHasStock(skuIdList);
		return R.ok().setData(hasStock);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	//@RequiresPermissions("ware:waresku:save")
	public R save(@RequestBody WareSkuEntity wareSku) {
		wareSkuService.save(wareSku);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	//@RequiresPermissions("ware:waresku:update")
	public R update(@RequestBody WareSkuEntity wareSku) {
		wareSkuService.updateById(wareSku);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	//@RequiresPermissions("ware:waresku:delete")
	public R delete(@RequestBody Long[] ids) {
		wareSkuService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
